package com.noahplat.user.controller;

import com.noahplat.common.utils.Result;
import com.noahplat.user.pojo.Sconfig;
import com.noahplat.user.service.SconfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;


@RequestMapping("/api/sconfig")
@RestController
@Validated
public class SconfigController {
    @Autowired
    private SconfigService mapkeyService;

    /**
     * 查询正在使用的key值ID
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:SconfigController:getMapnum')")
    @GetMapping("/getMapnum")
    public Result getMapnum(String type){
        return mapkeyService.getMapnum(type);
    }

    /**
     * 修改正在使用的key值ID
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:SconfigController:updateMapnum')")
    @PutMapping("/updateMapnum")
    public Result updateMapnum(Long id){
        return mapkeyService.updateMapnum(id);
    }

    /**
     * 分页获得高德key值信息
     * @param type
     * @param state
     * @param page
     * @param limit
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:SconfigController:getAdminMapkey')")
    @GetMapping("/getAdminMapkey")
    public Result getAdminMapkey(@RequestParam String type,@RequestParam String state, @RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10")Integer limit){
        return mapkeyService.getAdminMapkey(type, state, page, limit);
    }

    /**
     * 通过ID获得高德key详细信息
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:SconfigController:getMapkey')")
    @GetMapping("/getMapkey")
    public Result getMapkey(@NotNull(message = "ID不能为空 ") @Min(value = 1, message = "Id不能为空")Long id){
        return mapkeyService.getMapkey(id);
    }

    /**
     * 修改高德key值信息
     * @param sconfig
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:SconfigController:updateMapkey')")
    @PutMapping("/updateMapkey")
    public Result updateMapkey(@RequestBody Sconfig sconfig){
        return mapkeyService.updateMapkey(sconfig);
    }

    /**
     * 批量添加高德KEY信息
     * @param file
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:SconfigController:addMapExcel')")
    @PostMapping("/addMapExcel")
    public Result addMapExcel(@RequestParam(value = "file") MultipartFile file){
        return mapkeyService.addMapExcel(file);
    }

    /**
     * 删除高德key值
     * @param sconfig
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:SconfigController:delMapkey')")
    @DeleteMapping("/delMapkey")
    public Result delMapkey(@RequestBody Sconfig sconfig){
        return mapkeyService.delMapkey(sconfig);
    }
}
